package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.ApplyDto;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysMessageMapper;
import com.ruoyi.system.domain.SysMessage;
import com.ruoyi.system.service.ISysMessageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 系统内部消息通知Service业务层处理
 *
 * @author ruoyi
 * @date 2024-06-09
 */
@Service
public class SysMessageServiceImpl implements ISysMessageService
{
    @Autowired
    private SysMessageMapper sysMessageMapper;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询系统内部消息通知
     *
     * @param messageId 系统内部消息通知ID
     * @return 系统内部消息通知
     */
    @Override
    public SysMessage selectSysMessageById(Long messageId)
    {
        return sysMessageMapper.selectSysMessageById(messageId);
    }

    /**
     * 查询系统内部消息通知列表
     *
     * @param sysMessage 系统内部消息通知
     * @return 系统内部消息通知
     */
    @Override
    public List<SysMessage> selectSysMessageList(SysMessage sysMessage)
    {
        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
        //如果不是管理员就只能看自己管辖的社团部门申请
        if (!SecurityUtils.isAdmin(userId)) {
            sysMessage.setReceiveId(userId);
        }
        return sysMessageMapper.selectSysMessageList(sysMessage);
    }

    /**
     * 新增系统内部消息通知
     *
     * @param sysMessage 系统内部消息通知
     * @return 结果
     */
    @Override
    public int insertSysMessage(SysMessage sysMessage)
    {
        sysMessage.setCreateTime(DateUtils.getNowDate());
        return sysMessageMapper.insertSysMessage(sysMessage);
    }

    /**
     * 修改系统内部消息通知
     *
     * @param sysMessage 系统内部消息通知
     * @return 结果
     */
    @Override
    public int updateSysMessage(SysMessage sysMessage)
    {
        return sysMessageMapper.updateSysMessage(sysMessage);
    }

    /**
     * 批量删除系统内部消息通知
     *
     * @param messageIds 需要删除的系统内部消息通知ID
     * @return 结果
     */
    @Override
    public int deleteSysMessageByIds(Long[] messageIds)
    {
        return sysMessageMapper.deleteSysMessageByIds(messageIds);
    }

    /**
     * 删除系统内部消息通知信息
     *
     * @param messageId 系统内部消息通知ID
     * @return 结果
     */
    @Override
    public int deleteSysMessageById(Long messageId)
    {
        return sysMessageMapper.deleteSysMessageById(messageId);
    }

    @Override
    public List<SysMessage> selectSysMessageByReceiveId(Long receiveId) {
        return sysMessageMapper.selectSysMessageByReceiveId(receiveId);
    }

    @Override
    public int sendMessageToAdmin(ApplyDto applyDto) {
      
            // 创建一个新的消息实体
            SysMessage sysMessage = new SysMessage();
            sysMessage.setReceiveId(applyDto.getReceiveId());
            sysMessage.setDeptId(applyDto.getDeptId());
            sysMessage.setSendId(applyDto.getSendId());
            sysMessage.setMessage(applyDto.getMessage());
            sysMessage.setStatus(0);
            sysMessage.setCreateTime(new Date());

            // 将消息保存到数据库
           return sysMessageMapper.insertSysMessage(sysMessage);
        
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int approveJoinMass(SysMessage sysMessage) {
        SysUser sysUser = sysUserService.selectUserById(sysMessage.getSendId());
        sysUser.setDeptId(sysMessage.getDeptId());
        //更新用户所申请的社团
        sysUserService.updateUser(sysUser);
        //将申请消息改为已读
        return sysMessageMapper.approveJoinMass(sysMessage.getMessageId());
    }

}
